<?php
namespace App\Controllers\Index;
use App\Models\Peizhi\Peizhi;
use App\Models\Goods\Goods;

/**
*	首页接口获取
*/
class GetPeizhi extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }
	
	public function execute(){
		//查询peizhi表里所有首页的数据
		$type = (int)$this->request->get('type');
		if( !empty( $type ) ){
			$peizhi = Peizhi::find("type={$type}");
			$peizhi->data = json_decode($peizhi->data,true);
		}else{
			$oneTwoThere = Peizhi::findAll('type=1 or type = 2 or type = 3 or type = 5 or type = 6');
			//$peizhi['activity'] = Peizhi::findFirst('type=2')->data;
			//$peizhi['icon'] = Peizhi::findFirst('type=3');
			//爆款处理
				$baokuan = Peizhi::findFirst('type=4');	
			//爆款推荐
			//echo "<pre>";print_r($baokuan);
			$baokuanArr = json_decode($baokuan->data);
			
			foreach( $baokuanArr as $k=>$v ){
			//	echo "<pre>";print_r($v);
				$where[] = 'id ='.$v->gid;
				$baokuanOther[$v->gid] = $v;
			}	
			$peizhi['baokuan'] = Goods::findAll(array(implode(' or ', $where),"order" => "weight"));
			$peizhi['baokuan'] = $peizhi['baokuan']->toArray();
			foreach($peizhi['baokuan'] as $k=>$v){
				$peizhi['baokuan'][$k]['addr'] = $baokuanOther[$v['id']]->addr;
				$peizhi['baokuan'][$k]['img'] = $baokuanOther[$v['id']]->img;
				$peizhi['baokuan'][$k]['action'] = $baokuanOther[$v['id']]->action;
			}
			
			foreach($oneTwoThere as $k=>$v){
				if( $v->type == 1 ){
					$peizhi['banner'] = json_decode($v->data);
				}
				if( $v->type == 2 ){
					$peizhi['active'] = json_decode($v->data);
				}
				if( $v->type == 3 ){
					$peizhi['icon'] = json_decode($v->data);
				}
				if( $v->type == 5 ){
					$peizhi['hot'] = json_decode($v->data);
				}
				if( $v->type == 6 ){
					$peizhi['about']['data'] = json_decode($v->data);
				}
			}
		}
		
		


			
		
		return \App\Libs\Views\Json::view(0, '成功', $peizhi);	
	}
}
